package controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.DataAccess;

/**
 * Servlet implementation class Controller
 */
public class Controller extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Controller() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		if("createAsset".equals(request.getParameter("whereFrom")))
		{
			boolean requestStatus=DataAccess.createAsset(request.getParameter("name"), request.getParameter("description"), request.getParameter("createdOn"), (String)request.getAttribute("company_id"),(String)request.getParameter("ale-tag"));
			if(requestStatus)
			{
				request.setAttribute("message", "Asset Created Successfully !");
				response.getWriter().println("Asset Created !");
			}
			else
			{
				request.setAttribute("message", "Something Snapped! Couldn't Create Asset !");
				response.getWriter().println("Snapped !");
						}
		}
		
		if("createCompany".equals(request.getParameter("whereFrom")))
		{
			String name=(String)request.getParameter("name");
			String address=(String)request.getParameter("address");
			String phone=(String)request.getParameter("phone");
			String email=(String)request.getParameter("email");
			String date=(String)request.getParameter("dateCreated");
			
			boolean requestStatus=DataAccess.createCompany(name, address, phone, email, date);
			if(requestStatus)
			{
				response.getWriter().println("Company Created !");
			}
			else
			{
				response.getWriter().println("Something Snapped!");
			}
		}
		

		if("createUser".equals(request.getParameter("whereFrom")))
		{
			String name=(String)request.getParameter("name");
			String password=(String)request.getParameter("password");
			String employer=(String)request.getParameter("employer");
			boolean requestStatus=DataAccess.createUser(name,password,employer);
			if(requestStatus)
			{
				response.getWriter().println("User Created !");
			}
			else
			{
				response.getWriter().println("Something Snapped!");
			}
		}
		
		if("login".equals(request.getParameter("whereFrom")))
		{
			String name=(String)request.getParameter("name");
			String password=(String)request.getParameter("password");
			String company_id=DataAccess.login(name,password);
			 
			if(company_id!=null)
			{
				HttpSession session = request.getSession();
				session.setAttribute("company_id",company_id);
				session.setAttribute("user", name);
				response.getWriter().println("Login Success !");
			}
			else
			{
				response.getWriter().println("Something Snapped !");
			}
		}
	}

}
